<template>
  <el-dialog
    ref='drawer'
    :title='title'
    :visible.sync='drawer'
    destroy-on-close
    @close='clodeModel'
  >
    <div v-if='drawer' class='draw-body'>
      <div class='wise-form'>
        <el-form
          ref='ruleForm'
          :model='object'
          :rules='rules'
          :validate-on-rule-change='false'
          label-width='130px'
          label-suffix=':'
        >
          <el-form-item label='作业单位' prop='name'>
            <el-input
              v-model='object.name'
              placeholder='请输入作业单位'
              :maxlength='100'
              show-word-limit
              clearable
              autofocus
            />
          </el-form-item>
          <el-form-item label="项目地址" prop="area">
            <wise-region v-model="object.area" :obj-keys="[
                {
                  field: 'province',
                  key: 'province',
                  value: 'key',
                },
                {
                  field: 'provinceName',
                  key: 'province',
                  value: 'value',
                  tableShow: true,
                },
                {
                  field: 'city',
                  key: 'city',
                  value: 'key',
                },
                {
                  field: 'cityName',
                  key: 'city',
                  value: 'value',
                  tableShow: true,
                },
                {
                  field: 'country',
                  key: 'area',
                  value: 'key',
                },
                {
                  field: 'countryName',
                  key: 'area',
                  value: 'value',
                  tableShow: true,
                },
                {
                  field: 'street',
                  key: 'town',
                  value: 'key',
                },
                {
                  field: 'streetName',
                  key: 'town',
                  value: 'value',
                  tableShow: true,
                },
              ]" @on-value="setValue" />
          </el-form-item>
          <el-form-item label="详细地址" prop="address">
          <el-input
            placeholder="请输入详细地址"
            v-model="object.address"
            :maxlength="100"
            show-word-limit
            clearable
            autofocus
          />
        </el-form-item>
          <el-form-item label='项目开始时间' prop='bgTime'>
            <wise-date
              v-model='object.bgTime'
              type='datetime'
              format='yyyy-MM-dd HH:mm:ss'
            />
          </el-form-item>
          <el-form-item label='项目结束时间' prop='edTime'>
            <wise-date
              v-model='object.edTime'
              type='datetime'
              format='yyyy-MM-dd HH:mm:ss'
            />
          </el-form-item>
          <!-- <el-form-item label='工艺规程' prop='craftsId'>
           
              <el-select
              v-model='object.craftsId'
              class='detection'
              placeholder='请输入'
              multiple
            >
              <el-option
                v-for='item in craftsInfoList'
                :key='item.id'
                :label='item.craftsNumber'
                :value='item.id'
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label='动火级别' prop='dhjb'>
            <el-select v-model='object.dhjb'>
              <el-option
                v-for='item in dhListData'
                :key='item.id'
                :label='item.name'
                :value='item.name'
              />
            </el-select>
          </el-form-item>
          <el-form-item label='计划管理类型' prop='xffsid'>
            <el-select v-model='object.xffsid'>
              <el-option
                v-for='item in xffsListData'
                :key='item.id'
                :label='item.namex'
                :value='item.id'
              />
            </el-select>
          </el-form-item>
          <el-form-item label='视频选择' prop='videoids'>
            <el-select
              v-model='object.videoids'
              class='detection'
              placeholder='请输入'
              multiple
            >
              <el-option
                v-for='(item, index) in spListData'
                :key='index'
                :label='item.name'
                :value='item.id'
              />
            </el-select>
          </el-form-item>
          <el-form-item label='质量终端选择' prop='zlids'>
            <el-select
              v-model='object.zlids'
              class='detection'
              placeholder='请选择终端'
              multiple
            >
              <el-option
                v-for='(item, index) in zlListData'
                :key='index'
                :label='item.hjEquipmentInfo.name'
                :value='item.hjEquipmentInfo.id'
              />
            </el-select>
             
          </el-form-item>
          <el-form-item label='安全终端选择' prop='aqids'>
            <el-select
              v-model='object.aqids'
              class='detection'
              placeholder='请选择终端'
              
            >
              <el-option
                v-for='item in aqListData'
                :key='item.hjEquipmentInfo.id'
                :label='item.hjEquipmentInfo.name'
                :value='item.hjEquipmentInfo.id'
              />
            </el-select>
             
          </el-form-item>
          <el-form-item label='切断终端选择' prop='latitude'>
            <el-select
              v-model='object.qdids'
              class='detection'
              placeholder='请选择终端'
              multiple
            >
              <el-option
                v-for='(item, index) in qdListData'
                :key='index'
                :label='item.hjEquipmentInfo.name'
                :value='item.hjEquipmentInfo.id'
              />
            </el-select>
             
          </el-form-item>
          <el-form-item label='人员选择' prop='hgsl' >
            焊工数：<el-input-number
              v-model='object.hgsl'
              placeholder=''
              :maxlength='10'
              :min="0"
              
              :cobtrols="false"
              show-word-limitclearable
              autofocus
              style='width: 120px'
            />
            管工数：<el-input-number
              v-model='object.ggsl'
              placeholder=''
              :maxlength='10'
              :min="0"
              :cobtrols="false"
              show-word-limitclearable
              autofocus
              style='width: 120px'
            />
            安全员数：<el-input-number
              v-model='object.aqysl'
              placeholder=''
              :maxlength='10'
              :min="0"
              :cobtrols="false"
              show-word-limitclearable
              autofocus
              style='width: 120px'
            />
            <br />
            电工数：<el-input-number
              v-model='object.dgsl'
              placeholder=''
              :maxlength='10'
              :min="0"
              :cobtrols="false"
              show-word-limitclearable
              autofocus
              style='width: 120px'
            />
            其他工人数：<el-input-number
              v-model='object.qtsl'
              placeholder=''
              :maxlength='10'
              :min="0"
              :cobtrols="false"
              show-word-limitclearable
              autofocus
              style='width: 120px'
            />
          </el-form-item>
          <el-form-item label='大屏名称' prop='screenName'>
            <el-input
              v-model='object.screenName'
              placeholder='请输入大屏名称'
              :maxlength='100'
              show-word-limitclearable
              autofocus
            />
          </el-form-item>
          <el-form-item label='大屏LOGO' prop='logo'>
            <wise-file
              v-model='object.logo'
              upload-type='image'
              :limit='1'
              :max-file-size='100 * 1024 * 1024'
            />
          </el-form-item>

          <el-form-item label='动火 方案' prop='construction'>
            <wise-file
              v-model='object.construction'
              upload-type='file'
              :limit='1'
              :max-file-size='100 * 1024 * 1024'
            />
          </el-form-item>
          <el-form-item label='同步到云端' prop='tbyd'>
            <el-switch v-model="object.tbyd" :active-value="1"
            :inactive-value="0" >
                    
                  </el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type='primary' @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import WiseFile from '@/components/Crud/components/wiseFile';
import WiseRegion from '@/components/Crud/components/wiseRegion';
import WiseDate from '@/components/Crud/components/wiseDate';
import { postHandle } from '@/api';
export default {
  components: {
    WiseRegion,
    WiseDate,
    WiseFile,
  },
  data() {
    return {
      urls: {
        list: '/api/v1/welder-sbtx-second/project-info-comb/list',
        add: '/api/v1/welder-sbtx-second/project-info-comb/createKs',
        show: '/api/v1/welder-sbtx-second/project-info-comb/show/',
        update: '/api/v1/welder-sbtx-second/project-info-comb/updateKs',
      },
      title: '创建项目',
      showcturl: false,
      drawer: false,
      object: {
        id:"",
        zlwgs:4,
        hjwgs:1,
        entSpaceId: '',
        area:null,
        province:null,
        provinceName:null,
        city:null,
        cityName:null,
        country:null,
        countryName:null,
        street:null,
        streetName:null,
        spaceId: '',
        zlids:[],
        aqids:null,
        qdids:[],
        file: [],
        remark: null,
        welderNumber: '',
        bgTime: 0,
        edTime: 0,
        name: '',
        dhjb: '',
        dhjbName: '',
        craftsId: '',
        craftsName: '',
        logo: '',
        construction: '',
        screenName: '',
        hgsl: null,
        ggsl: null,
        tbyd:1,
        dgsl: null,
        aqysl: null,
        qtsl: null,
        zdids: [],
        videoids: [],
        xffsid: '',
        xffsname: '',
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入作业单位',
            trigger: 'blur',
          },
        ],
        welderNumber: [
          {
            required: true,
            message: '请输入项目编号',
            trigger: 'blur',
          },
        ],
        xffsid: [
          {
            required: true,
            message: '请选择项目计划',
            trigger: 'blur',
          },
        ],
        area: [
          {
            required: false,
            message: '请选择地址',
            trigger: 'blur',
          },
        ],  videoids: [
          {
            required: true,
            message: '请选择视频',
            trigger: 'blur',
          },
        ], zlids: [
          {
            required: true,
            message: '请选择质量终端',
            trigger: 'blur',
          },
        ], aqids: [
          {
            required: true,
            message: '请选择环境终端',
            trigger: 'blur',
          },
        ], hgsl: [
          {
            required: true,
            message: '请选择人员',
            trigger: 'blur',
          },
        ], construction: [
          {
            required: true,
            message: '请上传动火方案',
            trigger: 'blur',
          }, {
              pattern: /^.+(\.pdf)$/,
              message: '动火方案应为pdf格式'
            }
        ], dhjb: [
          {
            required: true,
            message: '请选择动火级别',
            trigger: 'blur',
          },
        ],
        address1: [
          {
            required: true,
            message: '请输入详细地址',
            trigger: 'blur',
          },
          
         // 5、计划管理、视频选择、质量终端、环境终端、人员选择、动火方案
        ],
        
        bgTime: [
          {
            required: true,
            message: '请选择项目开始时间',
            trigger: 'blur',
          },
        ],
        edTime: [
          {
            required: true,
            message: '请选择项目结束时间',
            trigger: 'blur',
          },
        ],
        hotLevel: [
          {
            required: true,
            message: '请输入动火等级',
            trigger: 'blur',
          },
        ],
        hotEntName: [
          {
            required: true,
            message: '请输入动火作业单位',
            trigger: 'blur',
          },
        ],
        repairMethod: [
          {
            required: true,
            message: '请输入生产计划',
            trigger: 'blur',
          },
        ],

        screenName: [
          {
            required: true,
            message: '请输入大屏名称',
            trigger: 'blur',
          },
        ],
      },
      //质量终端选项
      zlListData: [],
      //安全终端选项
      aqListData: [],
      //切断终端选项
      qdListData: [],
      //视频选项
      spListData: [],
      //动火选项
      dhListData: [],
      //计划选项
      jhListData: [],

      initiateProjectId: null,
    };
  },

  computed: {
    entSpaceId() {
      return this.$store.getters.entSpaceId;
    },
  },
  created() {
    //this.getHotPlan()
    this.getCraftsInfoList();
    this.getCraftsInfoListx();
  },
  methods: {
    //关闭弹框
    clodeModel() {
     
      this.$refs['ruleForm'].resetFields();
    },

    editHandle(row) {
      this.title='编辑项目';

      const req = {
        d: {
          filters: [
            {
              field: 'id',
              operator: 'EQUALS',
              value: row.id,
            },
            {
              field: 'st',
              operator: 'NE',
              value: 9,
            },
          ],
          page: {
            p: 1,
            ps: 9999,
          },
        },
      };

      postHandle('/api/v1/welder-sbtx-second/project-info-comb/toUpdate', req).then(
        (response) => {
          const data = response.result;
          this.object= data;
          this.object.area = {
            province: {
              key: this.object.province,
              value: this.object.provinceName
            },
            city: {
              key: this.object.city,
              value: this.object.cityName
            },
            area: {
              key: this.object.country,
              value: this.object.countryName
            },
            town: {
              key: this.object.street,
              value: this.object.streetName
            }
          }
        }
      );
     
      // this.fileList = [];
      // this.getObjectById(row.id);
      // this.getCraftsInfoList();
      this.drawer = true;
    },
    getObjectById(id) {
      this.fileList = [];
      postHandle(this.urls.show + id, {}).then((response) => {
        this.object = response.result;
        this.object.area = {
          province: {
            key: this.object.province,
            value: this.object.provinceName,
          },
          city: {
            key: this.object.city,
            value: this.object.cityName,
          },
          area: {
            key: this.object.country,
            value: this.object.countryName,
          },
          town: {
            key: this.object.street,
            value: this.object.streetName,
          },
        };
      });
    },
    getCraftsInfoListx() {


      const reqjh = {  d: { filters: [  
          //{ field: 'spaceId',  operator: 'EQUALS', value: this.$store.getters.entSpaceId, },
       //   { field: 'sp', operator: 'EQUALS',  value: 1, },
        ],
          page: {  p: 1,  ps: 9999, }, },
      };
      postHandle('/api/v1/welder-sbtx-second/repair-mode-comb/list', reqjh).then(
        (response) => {
          const data = response.result;
          this.jhListData = data;
        }
      );

      const reqdh = {  d: { filters: [ 
          {  field: 'pdCode', operator: 'EQUALS', value: 'DHJB', },
          {  field: 'st', operator: 'NE', value: 9, }, ],
          page: {  p: 1, ps: 9999, },
          retOnlySt: false,
          sorts: [ { field: 'ct', isDesc: true, }, ],
        },
      };
      postHandle('/api/v1/welder-sbtx-second/dictionary', reqdh).then(
        (res) => {
          this.dhListData = res.result;
        }
      );

      const reqsp = { d: { filters: [
            { field: 'pdCode', operator: 'EQUALS', value: 'DHJB', },
            {  field: 'st',  operator: 'NE', value: 9,  }, ],
          page: {  p: 1,  ps: 9999, },
          retOnlySt: false,
          sorts: [ { field: 'ct', isDesc: true, }, ],
        },
      };
      postHandle('/api/v1/welder-sbtx-second/video-comb/listqy', reqsp).then(
        (res) => {
          this.spListData = res.result;
        }
      );


      const reqzl = {  d: { filters: [  
          { field: 'entSpaceId',  operator: 'EQUALS', value:  this.$store.getters.entSpaceId, },
          { field: 'gatewayType',  operator: 'EQUALS', value: "welder", },
          { field: 'sp', operator: 'EQUALS',  value: 1, }, ],
          page: {  p: 1,  ps: 9999, }, },
      };
      postHandle('/api/v1/welder-sbtx-second/gateway-equipment-comb/list', reqzl).then(
        (response) => {
          const data = response.result;
          this.zlListData = data;
        }
      );

      const reqaq = {  d: { filters: [ 
        { field: 'entSpaceId',  operator: 'EQUALS', value:  this.$store.getters.entSpaceId, },
        { field: 'gatewayType',  operator: 'EQUALS', value:"safety", },
        {  field: 'st', operator: 'NE', value: 9, }, ],
          page: {  p: 1, ps: 9999, },
          retOnlySt: false,
          sorts: [ { field: 'ct', isDesc: true, }, ],
        },
      };
      postHandle('/api/v1/welder-sbtx-second/gateway-equipment-comb/list', reqaq).then(
        (res) => {
          this.aqListData = res.result;
        }
      );

      const reqqd = { d: { filters: [
            { field: 'entSpaceId',  operator: 'EQUALS', value:  this.$store.getters.entSpaceId, },
            { field: 'gatewayType',  operator: 'EQUALS', value: "shear", },
            {  field: 'st',  operator: 'NE', value: 9,  }, ],
          page: {  p: 1,  ps: 9999, },
          retOnlySt: false,
          sorts: [ { field: 'ct', isDesc: true, }, ],
        },
      };
      postHandle('/api/v1/welder-sbtx-second/gateway-equipment-comb/list', reqqd).then(
        (res) => {
          this.qdListData = res.result;
        }
      );
      const reqjhx = { d: { filters: [
            { field: 'spaceId',  operator: 'EQUALS', value:  this.$store.getters.entSpaceId, },
            //{ field: 'typeCode',  operator: 'EQUALS', value: "shear", },
            {  field: 'st',  operator: 'NE', value: 9,  }, ],
          page: {  p: 1,  ps: 9999, },
          retOnlySt: false,
          sorts: [ { field: 'ct', isDesc: true, }, ],
        },
      };
      postHandle(
        '/api/v1/welder-sbtx-second/repair-mode-comb/list',  reqjhx
      ).then((res) => {
        this.xffsListData = res.result;
      });
   



      // const req = {
      //   d: {
      //     filters: [
      //       {
      //         field: 'spaceId',
      //         operator: 'EQUALS',
      //         value: this.spaceId,
      //       },
      //       {
      //         field: 'sp',
      //         operator: 'EQUALS',
      //         value: 1,
      //       },
      //     ],
      //     page: {
      //       p: 1,
      //       ps: 9999,
      //     },
      //   },
      // };

      // const params1 = {
      //   d: {
      //     filters: [
      //       {
      //         field: 'pdCode',
      //         operator: 'EQUALS',
      //         value: 'DHJB',
      //       },
      //       {
      //         field: 'st',
      //         operator: 'NE',
      //         value: 9,
      //       },
      //     ],
      //     page: {
      //       p: 1,
      //       ps: 9999,
      //     },
      //     retOnlySt: false,
      //     sorts: [
      //       {
      //         field: 'ct',
      //         isDesc: true,
      //       },
      //     ],
      //   },
      // };

      // const params2 = {
      //   d: {
      //     filters: [
      //       {
      //         field: 'pdCode',
      //         operator: 'EQUALS',
      //         value: 'DHJB',
      //       },
      //       {
      //         field: 'st',
      //         operator: 'NE',
      //         value: 9,
      //       },
      //     ],
      //     page: {
      //       p: 1,
      //       ps: 9999,
      //     },
      //     retOnlySt: false,
      //     sorts: [
      //       {
      //         field: 'ct',
      //         isDesc: true,
      //       },
      //     ],
      //   },
      // };
      // const params3 = {
      //   d: {
      //     filters: [
      //       {
      //         field: 'pdCode',
      //         operator: 'EQUALS',
      //         value: 'DHJB',
      //       },
      //       {
      //         field: 'st',
      //         operator: 'NE',
      //         value: 9,
      //       },
      //     ],
      //     page: {
      //       p: 1,
      //       ps: 9999,
      //     },
      //     retOnlySt: false,
      //     sorts: [
      //       {
      //         field: 'ct',
      //         isDesc: true,
      //       },
      //     ],
      //   },
      // };
      // const params4 = {
      //   d: {
      //     filters: [
      //       {
      //         field: 'pdCode',
      //         operator: 'EQUALS',
      //         value: 'DHJB',
      //       },
      //       {
      //         field: 'st',
      //         operator: 'NE',
      //         value: 9,
      //       },
      //       {
      //         field: 'entSpaceId',
      //         operator: 'EQUALS',
      //         value: this.entSpaceId,
      //       },
      //     ],
      //     page: {
      //       p: 1,
      //       ps: 9999,
      //     },
      //     retOnlySt: false,
      //     sorts: [
      //       {
      //         field: 'ct',
      //         isDesc: true,
      //       },
      //     ],
      //   },
      // };
      // //exportytg: '/api/v1/welder-sbtx-second/project-plan/exportytg',
      // postHandle('/api/v1/welder-sbtx-second/project-plan/getPPlist', req).then(
      //   (response) => {
      //     const data = response.result;
      //     this.planListData = data;
      //   }
      // );
      // postHandle('/api/v1/welder-sbtx-second/dictionary', params1).then(
      //   (res) => {
      //     this.dhlvListData = res.result;
      //   }
      // );
      // postHandle(
      //   '/api/v1/welder-sbtx-second/repair-mode-comb/list',
      //   params2
      // ).then((res) => {
      //   this.xffsListData = res.result;
      // });
      // postHandle(
      //   '/api/v1/welder-sbtx-second/video-comb/listqy',
      //   params3
      // ).then((res) => {
      //   this.sxtListData = res.result;
      // });
      // postHandle(
      //   '/api/v1/welder-sbtx-second/hj-equipment-info-comb/list',
      //   params4
      // ).then((res) => {
      //   this.zdListData = res.result;
      // });
    },
    getCraftsInfoList() {
      const req = {
        d: {
          page: {
            p: 1,
            ps: 9999,
          },
        },
      };

      
      postHandle('/api/v1/welder-sbtx-second/crafts-info-comb/list', req).then(
        (response) => {
          const data = response.result;
          this.craftsInfoList = data;
        }
      );
    },
    setValue(objVal) {
      if (objVal && objVal.length > 0) {
        objVal.forEach((item) => {
          this.$set(this.object, item.field, item.value);
        });
      }
    },
    changeReportLevel(value, row) {
      
      if (value == 1) {
        this.showcturl = true;
      } else if (value == 0) {
        this.showcturl = false;
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url;
          if (this.object.id) {
            url = this.urls.update ;
          } else {
            url = this.urls.add;
          }
          postHandle(url, {
            obj: {
              ...this.object,
              initiateProjectId: this.initiateProjectId,
            },
          })
            .then((response) => {
              this.$message('操作成功');
              this.drawer = false;
            
              if (this.object.id) {
                this.$emit('refreshTable');
              } else {
                this.$router.push({
                  name: 'CreateProject',
                });
              }
            })
            .catch((error) => {
              const data = error.response.data;
              for (const key in data) {
                this.$message.error(this.$t(key + '.' + data[key]));
              }
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
};
</script>

<style lang='scss' scoped></style>
